package com.macro.mall.tiny.modules.pms.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.macro.mall.tiny.common.api.CommonResult;
import com.macro.mall.tiny.component.sms.SmsReceiver;
import com.macro.mall.tiny.modules.pms.model.PmsBrand;
import com.macro.mall.tiny.modules.pms.service.PmsBrandService;
import com.macro.mall.tiny.security.util.CharUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 品牌表 前端控制器
 * </p>
 *
 * @author tyrone
 * @since 2024-10-09
 */
@RestController
@Api(tags = "PmsBrandController")
@Tag(name = "PmsBrandController", description = "品牌管理")
@RequestMapping("/pms/pmsBrand")
public class PmsBrandController {

    @Autowired
    private PmsBrandService pmsBrandService;
    @Autowired
    private SmsReceiver smsReceiver;

    @ApiOperation(value = "前台获取所有品牌")
    @GetMapping("/listAll")
    public CommonResult<List<PmsBrand>> listBrand() {
        return CommonResult.success(pmsBrandService.list());
    }

    @PutMapping("/add")
    @ApiOperation(value = "后台添加品牌")
    public CommonResult<Void> addBrand(@RequestBody PmsBrand pmsBrand) {
        if (ObjectUtils.isEmpty(pmsBrand.getFirstLetter())){
            pmsBrand.setFirstLetter(CharUtil.getFirstLetter(pmsBrand.getName()));
        }
        pmsBrandService.save(pmsBrand);
        return CommonResult.success(null);
    }

    @PostMapping("/update/{brandId}")
    @ApiOperation(value = "后台修改品牌")
    public CommonResult<Void> updateBrand(@ApiParam("品牌id") @PathVariable(value = "brandId") Integer brandId, @RequestBody PmsBrand pmsBrand) {
        pmsBrand.setId(brandId);
        pmsBrandService.updateById(pmsBrand);
        return CommonResult.success(null);
    }


    @ApiOperation(value = "后台分页获取产品")
    @PostMapping("/page")
    public CommonResult<IPage<PmsBrand>> listBrand(@RequestBody PmsBrand pmsBrand) {
        QueryWrapper<PmsBrand> query = new QueryWrapper<>();
        if (pmsBrand.getName() != null) query.like("name", pmsBrand.getName());
        if (pmsBrand.getFirstLetter() != null) query.like("first_letter", pmsBrand.getFirstLetter());
        if (pmsBrand.getShowStatus() != null) query.eq("show_status", pmsBrand.getShowStatus());
        if (pmsBrand.getId() != null) query.eq("id", pmsBrand.getId());
        IPage<PmsBrand> result = pmsBrandService.page(new Page<>(pmsBrand.getPageNum(), pmsBrand.getPageSize(), Boolean.TRUE), query);
        return CommonResult.success(result);
    }

    @ApiOperation("后台批量删除品牌")
    @DeleteMapping("/delete")
    public CommonResult<Void> deleteBrand(@RequestParam("ids") List<Integer> ids) {
        pmsBrandService.removeByIds(ids);
        return CommonResult.success(null);
    }

}

